﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace Brjnk.GpxExplorer.UI.Controls
{
    public class ColorPicker : Control
    {
        static ColorPicker()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ColorPicker), new FrameworkPropertyMetadata(typeof(ColorPicker)));
        }

        public ColorPicker()
        {
            this.MouseDown += new MouseButtonEventHandler(ColorPicker_MouseDown);

            Text = Color.ToString();
            TextBrush = new SolidColorBrush(GetTextColor(Color));
        }

        private void ColorPicker_MouseDown(object sender, MouseButtonEventArgs e)
        {
            var window = new ColorDialog(this.Color);
            if (window.ShowDialog() ?? false)
            {
                this.SetCurrentValue(ColorProperty, window.Color);
            }
        }

        public Color Color
        {
            get { return (Color)GetValue(ColorProperty); }
            set { SetValue(ColorProperty, value); }
        }

        public static readonly DependencyProperty ColorProperty =
            DependencyProperty.Register("Color", typeof(Color), typeof(ColorPicker), new FrameworkPropertyMetadata(Colors.Black) { PropertyChangedCallback = ColorChanged, BindsTwoWayByDefault = true });

        public Brush ColorBrush
        {
            get { return (Brush)GetValue(ColorBrushProperty); }
            protected set { SetValue(ColorBrushPropertyKey, value); }
        }

        private static readonly DependencyPropertyKey ColorBrushPropertyKey =
            DependencyProperty.RegisterReadOnly("ColorBrush", typeof(Brush), typeof(ColorPicker), new PropertyMetadata(new SolidColorBrush(Colors.Black)));

        public static readonly DependencyProperty ColorBrushProperty = ColorBrushPropertyKey.DependencyProperty;

        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            protected set { SetValue(TextPropertyKey, value); }
        }

        private static readonly DependencyPropertyKey TextPropertyKey =
            DependencyProperty.RegisterReadOnly("Text", typeof(string), typeof(ColorPicker), new PropertyMetadata());

        public static readonly DependencyProperty TextProperty = TextPropertyKey.DependencyProperty;

        public Brush TextBrush
        {
            get { return (Brush)GetValue(TextBrushProperty); }
            protected set { SetValue(TextBrushPropertyKey, value); }
        }

        private static readonly DependencyPropertyKey TextBrushPropertyKey =
            DependencyProperty.RegisterReadOnly("TextBrush", typeof(Brush), typeof(ColorPicker), new PropertyMetadata());

        public static readonly DependencyProperty TextBrushProperty = TextBrushPropertyKey.DependencyProperty;

        private static void ColorChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            var target = sender as ColorPicker;
            target.SetValue(ColorBrushPropertyKey, new SolidColorBrush(target.Color));
            target.Text = target.Color.ToString();
            target.TextBrush = new SolidColorBrush(GetTextColor(target.Color));
        }

        private static Color GetTextColor(Color color)
        {
            if ((color.R * 0.299 + color.G * 0.587 + color.B * 0.114) > 128) return Colors.Black;
            return Colors.White;
        }
    }
}